﻿/// Copyright (c) 2012 John Tear
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
/// and associated documentation files (the "Software"), to deal in the Software without restriction, 
/// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
/// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
/// subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial 
/// portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
/// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 
/// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
/// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
/// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
///

using System;
using System.Collections.Generic;
using System.Text;

using com.feelgoodsoftware.sharptftp.utilities;

namespace com.feelgoodsoftware.sharptftp
{
    public class TftpDataPacket : TftpBasePacket
    {
        /// <summary>
        /// The sequence/block number.
        /// </summary>
        public short BlockNumber { get; set; }

        /// <summary>
        /// The data in the packet.
        /// </summary>
        public byte[] Data { get; set; }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="Bytes">The bytes received on the network wire.</param>
        public TftpDataPacket(byte[] Bytes)
        {
            // record the correct op code
            OpCode = (TftpOpCode)Bytes[1];

            // grab the block number
            if (BitConverter.IsLittleEndian)
            {
                byte temp = Bytes[2];
                Bytes[2] = Bytes[3];
                Bytes[3] = temp;
            }
            

            BlockNumber = BitConverter.ToInt16(Bytes, 2);

            // grab the data field in the packet
            Data = new byte[Bytes.Length - 4];
            for (int i = 4; i < Bytes.Length; i++)
            {
                Data[i - 4] = Bytes[i];
            }
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="BlockNumber">The block number.</param>
        /// <param name="Data">The data in the packet.</param>
        public TftpDataPacket(short BlockNumber, byte[] Data)
        {
            this.OpCode = TftpOpCode.DATA;
            this.BlockNumber = BlockNumber;
            this.Data = Data;
        }

        public override byte[] ToByteArray()
        {
            byte[] returnArray = new byte[2] { 0, (byte)OpCode };
            returnArray = ByteArray.Combine(returnArray, BitConverter.GetBytes(BlockNumber));
            returnArray = ByteArray.Combine(returnArray, Data);

            if (BitConverter.IsLittleEndian)
            {
                byte temp = returnArray[2];
                returnArray[2] = returnArray[3];
                returnArray[3] = temp;
            }

            return returnArray;
        }
    }
}
